package me.zhengjie.modules.sync.processing;

import com.alibaba.fastjson.JSON;
import com.qiaofang.applet.common.constant.SyncTypeEnum;
import com.qiaofang.applet.service.property.domain.PropertyVr;
import com.qiaofang.applet.service.property.domain.SyncDataQueue;
import com.qiaofang.applet.service.property.service.PropertyVrService;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.sync.ProcessingBase;
import me.zhengjie.utils.ImageUtil;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 房源VR数据处理
 * @author QF2019
 */
@Slf4j
@Service
public class PropertyVrSyncProcessing extends ProcessingBase {

    public PropertyVrSyncProcessing(){
        setDataType(SyncTypeEnum.PRO_VR.getSyncType());
    }

    @Autowired
    PropertyVrService propertyVrService;

    @Override
    protected void processing(SyncDataQueue syncDataQueue) {
        String batchContent=syncDataQueue.getBatchContent();
        List<PropertyVr> list= JSON.parseArray(batchContent, PropertyVr.class);
        if(null==list){
            log.info("processing PropertyVr is null");
            return;
        }
        list.stream().forEach(item->{
            savePropertyVr(item,syncDataQueue);
        });
    }

    private void savePropertyVr(PropertyVr item,SyncDataQueue syncDataQueue){
        PropertyVr propertyVr;
        if(syncDataQueue.getV20Flag() != null && syncDataQueue.getV20Flag()) {
            propertyVr = propertyVrService.findByVrUuidAndPropertyUuidAndCompanyUuid(item.getVrUuid(), item.getPropertyUuid(), syncDataQueue.getCompanyUuid());
        } else {
            propertyVr = propertyVrService.findByVrIdAndPropertyUuidAndCompanyUuid(item.getVrId(), item.getPropertyUuid(), syncDataQueue.getCompanyUuid());
        }
        if(!Objects.isNull(item.getDeleted()) && item.getDeleted()){
            if (!Objects.isNull(propertyVr)) {
                propertyVrService.deleteById(propertyVr.getId());
            }
            return;
        }
        if(Objects.isNull(propertyVr)){
            propertyVr=new PropertyVr();
        }
        //复制属性
        String[] ignoreProperties=new String[] {"id","createBy","updateBy","createTime","updateTime"};
        BeanUtils.copyProperties(item,propertyVr,ignoreProperties);
        propertyVr.setCompanyUuid(syncDataQueue.getCompanyUuid());
        propertyVr.setCityCode(syncDataQueue.getCityCode());
        propertyVr.setCorporationCode(syncDataQueue.getCorporationCode());
        propertyVr.setUpdateTime(new Date());
        propertyVr.setRelationUpdate(true);
        propertyVr.setLastSyncDate(new Date(System.currentTimeMillis() - DateUtil.DAY_MILLISECONDS));
        replaceDomain(propertyVr);
        try {
            propertyVrService.save(propertyVr);
        } catch (Exception e) {
            log.error("savePropertyVr error",e);
            //记录错误日志
            this.saveErrorLog(JSON.toJSONString(propertyVr),e.getMessage(),syncDataQueue);
        }
    }

    //替换域名
    private void replaceDomain(PropertyVr propertyVr){
        String roamViewUrl = propertyVr.getRoamview();
        if(!StringUtils.isEmpty(roamViewUrl) && StringUtils.startsWithIgnoreCase(roamViewUrl,ImageUtil.HTTP)){
            roamViewUrl = StringUtils.replace(roamViewUrl,ImageUtil.HTTP, ImageUtil.HTTPS);
            propertyVr.setRoamview(roamViewUrl);
        }
    }
}
